package util;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.*;
import jxl.write.Number;
import obj.Accum;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ArtExtramobi {
    public static int row = 0;

    public static void main(String[] args) throws IOException, WriteException {
        String file = "data/products.xls";
        File xlsFile = new File(file);
        WorkbookSettings s = new WorkbookSettings();
        s.setLocale(new Locale("ru", "RU"));
        s.setEncoding("KOI8-R");
        WritableWorkbook workbook = Workbook.createWorkbook(xlsFile, s);

        workbook.createSheet("Товары", 0);
        WritableSheet sheet = workbook.getSheet(0);

        addHeader(sheet);

        addAccums(sheet);

        workbook.write();
        workbook.close();
    }

    private static void addAccums(WritableSheet sheet) throws IOException, WriteException {
        List<Accum> accums = Accum.parseAccums();
        List<String> uniqueNames = new ArrayList<String>();
        for (Accum accum : accums) {
            String model = filterAccumModel(accum.brandShort, accum.model, accum.devices);
            String name = accum.brandShort + "-" + model;
            if (uniqueNames.contains(name)) {
                System.out.println("Rejected, not unique: " + name);
                continue;
            }
            System.out.println(name);
            addAccum(accum, name, sheet);
            uniqueNames.add(name);
        }
    }

    private static String filterAccumModel(String brand, String model, String[] models) {
        if (brand.equals("A")) {
            model = model.replace("iPhone ", "");
        } else if (brand.equals("B")) {
            model = model.replace("-", "");
//            model = model.replace("M-", "");
//            model = model.replace("C-", "");
//            model = model.replace("F-", "");
//            model = model.replace("J-", "");
        } else if (brand.equals("H")) {
            model = model.replace("BA ", "");
            String[] fields = model.split("\\s");
            if (fields.length > 1) {
                model = fields[0];
            }
        } else if (brand.equals("L")) {
            model = model.replace("LGLP-", "");
            model = model.replace("BSL-", "");
            model = model.replace("BST-", "");
            model = model.replace("LGTL-GKIP-", "");
            model = model.replace("LGIP-", "");
            model = model.replace("BL-", "");
        } else if (brand.equals("N")) {
            model = model.replace("-", "");
            model = model.replace("-", "");
        } else if (brand.equals("SE")) {
            model = model.replace("-", "");
//            model = model.replace("BST-", "");
//            model = model.replace("EP-", "");
//            model = model.replace("BA-", "");
        }
        if (model.length() > 5) {
            model = models[0] + "a";
        }
        if (model.length() > 5) {
            model = model.split("\\s")[0];
            if (!model.endsWith("a")) {
                model = model + "a";
            }
        }
        return model;
    }

    public static void addHeader(WritableSheet sheet) throws WriteException {
        WritableCell cell = new Label(0, 0, "Артикул");
        sheet.addCell(cell);
        cell = new Label(1, 0, "Название");
        sheet.addCell(cell);
        cell = new Label(2, 0, "Количество");
        sheet.addCell(cell);
        cell = new Label(3, 0, "Всего");
        sheet.addCell(cell);
        cell = new Label(4, 0, "Цена (руб)");
        sheet.addCell(cell);
    }

    public static void addAccum(Accum accum, String articul, WritableSheet sheet) throws WriteException {
        row++;
        WritableCell cell = new Label(0, row, articul);
        sheet.addCell(cell);
        cell = new Label(1, row, "Аккумулятор " + accum.name);
        sheet.addCell(cell);
        cell = new Number(2, row, accum.count);
        sheet.addCell(cell);
        cell = new Number(3, row, accum.count);
        sheet.addCell(cell);
        cell = new Number(4, row, accum.price);
        sheet.addCell(cell);
    }
}
